<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog v-model="openFlag" :title="title" width="400" append-to-body>
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px" class="dialog-form-two">
        <el-row>
          <el-col :span="24">
            <el-form-item label="登录名" prop="userName">
              <el-input :disabled="isEdit" v-model.trim="formData.userName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="真实姓名" prop="nickName">
              <el-input v-model.trim="formData.nickName" placeholder="请输入" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="电话" prop="phonenumber">
              <el-input v-model.trim="formData.phonenumber" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model.trim="formData.email" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model.trim="formData.remark" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script setup>
import api from "./api.js";

const { proxy } = getCurrentInstance();

const emit = defineEmits(['sub']);
let organizationId = inject('organizationId');

const openFlag = ref(false);
const isEdit = ref(false);
const title = computed(() => {
  return isEdit.value ? '编辑' : '新增';
});

const form = ref();

const initFormData = {
  id: null,
  userName: null,
  nickName: null,
  phonenumber: null,
  email: null,
  remark: null
};

const formData = reactive({ ...initFormData });

const rules = {
  userName: [
    { required: true, message: "登录名不能为空", trigger: "blur" }
  ],
  nickName: [
    {
      required: true,
      message: "真实姓名不能为空",
      trigger: "blur"
    },
    {
      pattern: /^[\u4e00-\u9fa5]{2,4}$/, 
      message: "真实姓名必须是2到4位中文字符",
      trigger: "blur"
    }
  ],
  phonenumber: [
    { required: true, message: "电话不能为空", trigger: "blur" },
    {
      pattern: /^[1][3-9][0-9]{9}$/, 
      message: "请输入有效的11位手机号码",
      trigger: "blur"
    }
  ],
  email: [
    { required: true, message: "邮箱不能为空", trigger: "blur" },
    {
      pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, 
      message: "请输入有效的邮箱地址",
      trigger: "blur"
    }
  ],
};

function reset() {
  Object.assign(formData, initFormData);
}

function open(row) {
  reset();
  Object.assign(formData, row);
  if (row) {
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
  openFlag.value = true;
}

function close() {
  openFlag.value = false;
}

function getParams() {
  const params = { ...formData };
  if (organizationId.value) {
    const segs = organizationId.value.split('-');
    params.companyId = segs[0];
    params.deptId = segs[1];
  }

  params.organizationId = organizationId;
  return params;
}

function submitForm() {
  form.value.validate(valid => {
    if (valid) {
      const params = getParams();
      if (isEdit.value) {
        api.update(params).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          close();
          emit("sub");
        });
      } else {
        api.add(params).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          close();
          emit("sub");
        })
      }
    }
  });
}

defineExpose({
  open
})
</script>


